﻿using BoKeYuanApp.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using BoKeYuanApp.Model;

namespace BoKeYuanApp.View
{
    /// <summary>
    /// AppStatsEntryView.xaml 的交互逻辑
    /// </summary>
    public partial class AppStatsEntryView : Window
    {
        public AppStatsEntryView()
        {
            InitializeComponent();
            statisticsModel = new StatisticsModel();
            using (BoKeYuanDbEntities dbEntities = new BoKeYuanDbEntities())
            {
                var recentStats = dbEntities.Statistics.OrderByDescending(i => i.recordTime).Take(5);
                foreach (Statistics stat in recentStats)
                {
                    AppStaticsRecentListBox.Items.Add($"{stat.recordTime} 博客-{stat.blog} 随笔-{stat.jotting} 文章-{stat.article} 评论-{stat.comment}");
                }
            }
                
        }

        private StatisticsModel statisticsModel;
        private void InquireButton_OnClick(object sender, RoutedEventArgs e)
        {
            BoKeYuanHelper boKeYuanHelper = new BoKeYuanHelper();
            boKeYuanHelper.demo();
            try
            {
                var appStat = boKeYuanHelper.ParseAggStats();
                var personStat = boKeYuanHelper.ParsePersonStats();
                statisticsModel.blog = appStat.Item1;
                statisticsModel.jotting = appStat.Item2;
                statisticsModel.article = appStat.Item3;
                statisticsModel.comment = appStat.Item4;
                statisticsModel.recordTime = DateTime.Now;
                Console.WriteLine(appStat);
                Console.WriteLine(personStat);//
                AppStaticsTextBox.Text = appStat.ToString();
            }
            catch (WebException exception)
            {
                MessageBox.Show(exception.ToString());

            }
          
        }

        private void EntryButton_OnClick(object sender, RoutedEventArgs e)
        {
            using (BoKeYuanDbEntities dbEntities = new BoKeYuanDbEntities())
            {
                Statistics statistics = new Statistics()
                {
                    blog = statisticsModel.blog,
                    jotting = statisticsModel.jotting,
                    article = statisticsModel.article,
                    comment = statisticsModel.comment,
                    recordTime = statisticsModel.recordTime,
                };
                statistics.guid = Guid.NewGuid().ToString("N");
                dbEntities.Statistics.Add(statistics);
                dbEntities.SaveChanges();
            }
            MessageBox.Show("录入成功");
        }
    }


   
}
